package com.woniuxy.controller;

import com.woniuxy.entity.Car;
import com.woniuxy.service.CarService;
import com.woniuxy.util.CodeUtils;
import com.woniuxy.util.Page;
import com.woniuxy.util.Result;
import com.woniuxy.vo.CarVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CarController {
    @Resource
    private CarService carService;

    @RequestMapping("/car/selectCar")
    public Result selectCar(Integer index, Integer number, String campName, String carName) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("index", index);
        map.put("number", number);
        map.put("campName", campName);
        map.put("carName", carName);
        List<CarVO> list = carService.selectCarByExample(map);
        int dataCount = carService.countCarByExample(map);
        int pageCount = dataCount % number == 0 ? dataCount / number : dataCount / number + 1;
        Page page = new Page();
        page.setPageIndex(index);
        page.setPageCount(pageCount);
        page.setCount(dataCount);
        page.setNumber(number);
        System.out.println(list);
        return new Result("200", "success", page, list);
    }

    //    更新
    @RequestMapping("/car/updateCar")
    public Result updateCar(CarVO carVo) throws Exception {
        Car car = new Car();
        car.setCarId(carVo.getCarId());
        car.setCampId(carVo.getCampId());
        car.setLicense(carVo.getLicense());
        car.setTypeId(carVo.getTypeId());
        car.setCarName(carVo.getCarName());
        car.setStatus(carVo.getStatus());
        carService.updateCar(car);
        return new Result("200", "success", null, null);
    }

    //    删除
    @RequestMapping("/car/deleteCar")
    public Result deleteCar(Integer carId) throws Exception {
        carService.deleteCar(carId);
        return new Result("200", "success", null, null);
    }

    //    查询所有款式
    @RequestMapping("/car/selectAllCarType")
    public Result selectAllCarType() throws Exception {
        return new Result("200", "success", null, carService.selectAllCarType());
    }
//    新增
    @RequestMapping("/car/insertCar")
    public Result insertCar(CarVO carVO) throws Exception{
        Car car = new Car();
        car.setCampId(carVO.getCampId());
        car.setCarName(carVO.getCarName());
        car.setTypeId(carVO.getTypeId());
        car.setStatus(0);
        car.setLicense(carVO.getLicense());
        int i = carService.insertCar(car);
        //获取自增id 生成二维码
        String path = CodeUtils.makeCode(car.getCarId());
        car.setCodeUrl(path);
        carService.insertCarById(car);
        return new Result("200","success",null,null);
    }
}
